package iscript.gameFramework.NetService.util 
{
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	import iscript.gameFramework.NetService.data.KeyTable;
	import be.boulevart.as3.security.RC4;
	import iscript.gameFramework.NetService.ServiceProxy;
	
	
	public class ProtocolUtils 
	{
		
		//组包
		//$OGPack 不包含包长度、只有主包头子包头和包文的未加密包
		/*static public function composePack($OGPack:ByteArray):ByteArray
		{ 
			var keyIndex:int = KeyTable.getInstance().randomIndex()
			var key:String = KeyTable.getInstance().content[keyIndex];
			
			var crypticPack:ByteArray = RC4.encryptByte($OGPack, key);
			crypticPack.endian = Endian.LITTLE_ENDIAN;
			crypticPack.position = 0;
			
			var result:ByteArray = new ByteArray;
			result.endian = Endian.LITTLE_ENDIAN;
			result.writeByte(keyIndex);//写入key index
			result.writeShort(3 + crypticPack.length);//写入包长度 = keyindex一个字节 + 包长度两字节 + 密码包长度
			result.writeBytes(crypticPack);
			return result;
		}*/
		

		/**
		 * 拆包 去掉了KeyIndex 和  PackLen  保留MainID  ChildID  body数据
		 * @param	$crypticPack
		 * @return
		 */
		static	public  function breakdownPack($crypticPack:ByteArray):ByteArray
		{
			$crypticPack.position = 0;
			var keyIndex:uint = $crypticPack.readUnsignedByte();
			var key:String = KeyTable.getInstance().content[keyIndex];
			var mainPack:ByteArray = new ByteArray();
			mainPack.endian = Endian.LITTLE_ENDIAN;
			mainPack.writeBytes($crypticPack, 3);
			
			if (ServiceProxy.instance.encrypt) {
			   //服务器端加密状态下做解密处理
			   return RC4.decryptByte(mainPack, key);
			}
			  return mainPack;
		}
	}

}